package paperfly.game.state;

import java.nio.FloatBuffer;

import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.shape.Cylinder;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.AlphaState;
import com.jme.scene.state.LightState;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jme.util.geom.BufferUtils;

public class SetorState extends SimpleGame {
	private int textureWidth;
	private int textureHeight;
	private Quaternion rotQuat = new Quaternion();
	private Vector3f axis = new Vector3f(1, 1, 0);
	private Cylinder cylinder;
	private float angle = 0;
	private Node hudNode;

	private float getUForPixel(int xPixel) {
		return (float) xPixel / textureWidth;
	}

	private float getVForPixel(int yPixel) {
		return 1f - (float) yPixel / textureHeight;
	}

	@Override
	protected void simpleUpdate() {
		if (timer.getTimePerFrame() < 1) {
			angle = angle + timer.getTimePerFrame();
		}

		rotQuat.fromAngleAxis(angle, axis);
		cylinder.setLocalRotation(rotQuat);
		// TODO Auto-generated method stub
		super.simpleUpdate();
	}

	@Override
	protected void simpleInitGame() {

		display.setTitle("HUD Tutorial 1");

		/* create a rotating cylinder so we have something in the background */
		cylinder = new Cylinder("Cylinder", 6, 18, 5, 10);
		cylinder.setModelBound(new BoundingBox());
		cylinder.updateModelBound();

		MaterialState ms = display.getRenderer().createMaterialState();
		ms.setAmbient(new ColorRGBA(1f, 0f, 0f, 1f));
		ms.setDiffuse(new ColorRGBA(1f, 0f, 0f, 1f));

		/* has been depricated */
		// ms.setAlpha(1f);
		ms.setEnabled(true);
		cylinder.setRenderState(ms);
		cylinder.updateRenderState();

		rootNode.attachChild(cylinder);
		hudNode = new Node("hudNode");
		TextureState ts = display.getRenderer().createTextureState();
		ts.setTexture(TextureManager.loadTexture(
				"images/maps/map1/HUD/setor.png", Texture.MM_LINEAR,
				Texture.FM_LINEAR, 1.0f, true));
		textureWidth = ts.getTexture().getImage().getWidth();
		textureHeight = ts.getTexture().getImage().getHeight();
		ts.setEnabled(true);
		Quad hudQuad = new Quad("hud", 40f, 40f);
		hudQuad.setRenderQueueMode(Renderer.QUEUE_ORTHO);

		hudQuad.setLocalTranslation(new Vector3f(textureWidth, textureHeight,0));

		/* does not work to disable light under v0.10 */
		// LightState ls = display.getRenderer().createLightState();
		// ls.setEnabled(false);
		// hudQuad.setRenderState(ls);
		hudQuad.setLightCombineMode(LightState.OFF);
		hudQuad.updateRenderState();
		FloatBuffer texCoords = BufferUtils.createVector2Buffer(4);
		texCoords.put(getUForPixel(0)).put(getVForPixel(0));
		texCoords.put(getUForPixel(0)).put(getVForPixel(40));
		texCoords.put(getUForPixel(40)).put(getVForPixel(40));
		texCoords.put(getUForPixel(40)).put(getVForPixel(0));
		hudQuad.setTextureBuffer(0, texCoords);
		hudQuad.setRenderState(ts);
		AlphaState as = display.getRenderer().createAlphaState();
		as.setBlendEnabled(true);

		as.setSrcFunction(AlphaState.SB_SRC_ALPHA);
		as.setDstFunction(AlphaState.DB_ONE_MINUS_SRC_ALPHA);
		as.setTestEnabled(false);
		as.setEnabled(true);
		hudQuad.setRenderState(as);
		hudNode.attachChild(hudQuad);
		rootNode.attachChild(hudNode);

	}

	public static void main(String[] args) {
		SimpleGame game = new SetorState();
		game.start();
	}
}
